package com.ciaojian.client.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ciaojian.client.service.CouponCodeServiceImpl;
import com.ciaojian.client.service.CouponCodeTakeRecordServiceImpl;
import com.ciaojian.client.service.ProductGroupServiceImpl;
import com.ciaojian.core.base.annotation.ApiIdempotent;
import com.ciaojian.core.enums.business.CouponCodeOriginEnum;
import com.ciaojian.core.exception.EntityNotFoundException;
import com.ciaojian.core.mapper.CouponCodeMapper;
import com.ciaojian.core.mapper.OrderMapper;
import com.ciaojian.core.mapper.UserMapper;
import com.ciaojian.core.model.*;
import com.ciaojian.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author oxiny
 */
@Api(tags = "抖音模块")
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("/dou-yin")
public class DouyinController {
    @Resource
    private CouponCodeServiceImpl couponCodeService;
    @Resource
    private CouponCodeTakeRecordServiceImpl couponCodeTakeRecordService;
    @Resource
    private ProductGroupServiceImpl productGroupService;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CouponCodeMapper couponCodeMapper;

    @ApiOperation("抖音核销获得券")
//    @ApiIdempotent
    @PutMapping("/write-off")
    public Result giveCodeByWriteOff(@NotNull Integer userId,
                                     @NotNull Integer workerId,
                                     @NotNull Integer storeId,
                                     @NotNull Integer productGroupId,
                                     @NotBlank String douyinCode) {
        if (productGroupId != 21 && productGroupId != 20 && productGroupId != 22) {
            Integer count = couponCodeTakeRecordService.lambdaQuery()
                    .eq(CouponCodeTakeRecord::getUserId, userId)
                    .eq(CouponCodeTakeRecord::getOrigin, CouponCodeOriginEnum.DOUYIN_WRITE_OFF)
                    .count();
            Assert.isTrue(count == 0, "您已领取!");
        }

        User user = userMapper.selectById(userId);
        //已经登陆过潮剪小程序的用户
        QueryWrapper<CouponCode> couponCodeQueryWrapper = new QueryWrapper<>();
        couponCodeQueryWrapper.eq("user_id",user.getId());
        couponCodeQueryWrapper.eq("coupon_id",469);
        List<CouponCode> couponCodes = couponCodeMapper.selectList(couponCodeQueryWrapper);
        if (couponCodes.size()>0) {
            return Result.error("用户已有抖音券");

        }
        try {
            QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
            orderQueryWrapper.eq("youzan_code", douyinCode);
            List<Order> orders = orderMapper.selectList(orderQueryWrapper);
            if (orders.size()>0) {
                return Result.ok("当前抖音码已经使用","当前抖音码已经使用");
            }
        }catch (Exception e){
            return Result.error("系统错误");
        }



        // 购买的产品组
        ProductGroup productGroup = productGroupService.getById(productGroupId).orElseThrow(
                () -> new EntityNotFoundException(ProductGroup.class, "id", productGroupId)
        );
        couponCodeService.douyinWriteOff(userId, workerId, storeId, productGroup, douyinCode,null,null);
        return Result.ok();
    }
}
